package lgkj.meicqt20.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lgkj.meicqt20.R;
import lgkj.meicqt20.bean.GameEnitity;
import lgkj.meicqt20.bean.ShopEnitity;
import lgkj.meicqt20.utils.CommUtils;

/**
 * Created by Administrator on 2017/8/17 0017.
 */

public class GameAdapter extends BaseAdapter {
    private Context context;

    private List<GameEnitity.DataBean> dataBeen;
    private List<ShopEnitity.DataBean> shopEnitity;
    private int position = -1;
    private boolean type = true;   //true 游戏;false 二维码

    public void setPosition(int position) {
        this.position = position;
        Log.d("GameAdapter", "setPosition: " + position);
    }

    public int getPosition() {
        return position;
    }

    public GameAdapter(Context context) {
        this.context = context;
        dataBeen = new ArrayList<>();
        shopEnitity = new ArrayList<>();
        type = true;
    }

    public void setData(List<GameEnitity.DataBean> data) {
        this.dataBeen = data;
        type = true;
    }

    public void setShopData(List<ShopEnitity.DataBean> data) {
        this.shopEnitity = data;
        type = false;
    }

    @Override
    public int getCount() {
        if (type)
            return dataBeen.size();
        else
            return shopEnitity.size();
    }

    @Override
    public Object getItem(int i) {
        return dataBeen.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(context).inflate(R.layout.item_game, null);
            holder.video_pic = view.findViewById(R.id.video_pic);
            holder.video_name = view.findViewById(R.id.video_name);
            holder.one = view.findViewById(R.id.one);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        ViewGroup.LayoutParams params = (holder.video_pic).getLayoutParams();
        params.height = 470;
        if (type) {
            holder.video_name.setVisibility(View.GONE);
            Log.d("gameAdapter", "getView: " + dataBeen.get(i).getPuzzle_url());
            CommUtils.LoadImage(context, holder.video_pic, dataBeen.get(i).getPuzzle_url(), 0);
        } else {
            CommUtils.LoadImage(context, holder.video_pic, shopEnitity.get(i).getImg_url(), 0);
            holder.video_name.setVisibility(View.VISIBLE);
            holder.video_name.setText(shopEnitity.get(i).getName());
        }
//        holder.video_pic.setLayoutParams(params);
        if (i == position) {
            holder.one.setBackgroundResource(R.drawable.yxrydybk);
        } else {
            holder.one.setBackgroundResource(0);
        }
        return view;
    }

    class Holder {
        ImageView video_pic;
        LinearLayout one;
        TextView video_name;
    }
}
